﻿@{
    Layout = null;
}
@using ActivityReservation.Services
@using System.Net
@using System.Net.Sockets
@inject IApplicationSettingService applicationSettings
<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width" />
    <title>登录 - 后台管理系统</title>
    <link rel="stylesheet" href="~/Content/bootstrap.min.css" />
</head>
<body>
    <div class="container" style="max-width: 30em;">
        <header>
            <h1 class="h1">
                登录<small>后台管理系统</small>
            </h1>
        </header>
        <section>
            <form class="form-signin" style="margin: 2em auto">
                <div class="form-group">
                    <label for="inputUsername" class="sr-only">用户名</label>
                    <input type="text" id="inputUsername" name="UserName" class="form-control" placeholder="用户名" required="required" autofocus>
                </div>
                <div>
                    <label for="inputPassword" class="sr-only">密码</label>
                    <input type="password" id="inputPassword" name="Password" class="form-control" placeholder="密码" required="required">
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" value="remember-me" name="rememberMe" id="rememberMe"> Remember me
                    </label>
                </div>
                <button class="btn btn-lg btn-primary btn-block btnSubmit" type="button" id="btnLogin" disabled="disabled">登录</button>
            </form>
        </section>
        <footer>
            <p>&copy; @DateTime.UtcNow.Year - @applicationSettings.GetSettingValue("SystemTitle") &nbsp;&nbsp; HostIp: <strong>@(Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(_ => _.AddressFamily == AddressFamily.InterNetwork)) </strong></p>
        </footer>
    </div>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Content/layer/layer.js"></script>
    <!-- <script src="~/Scripts/recaptchaHelper.js"></script> -->
    <script>
    var loading = layer.load(2);
        document.onreadystatechange = function () {
            if (document.readyState === "complete") { //当页面加载状态为完全结束时进入
                layer.close(loading);//当页面加载完成后将loading页隐藏
                $(".btnSubmit").removeAttr("disabled");
            }
        };
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    function isFormValid() {
        var username = $("#inputUsername").val();
        var password = $("#inputPassword").val();
        if (username && password) {
            if (username.length > 2 && username.length <= 8) {
            } else {
                layer.msg("用户名不合法，用户名长度至少两个字符，最多8个字符");
                return false;
            }
            if (password.length >= 6 && password.length <= 20) {
            } else {
                layer.msg("密码不合法，密码长度至少6个字符，最多20个字符");
                return false;
            }
            var recaptcha = '111';
            if (!recaptcha) {
                layer.msg('验证码输入有误');
                return false;
            }
            return true;
        } else {
            layer.msg("登录信息不完整");
            return false;
        }
    }

    function Login() {
        if (!isFormValid()) {
            return;
        }
        var username = $("#inputUsername").val();
        var password = $("#inputPassword").val();
        //var gResponse = recaptchaHelper.getRecaptchaResponse();
        //if (!gResponse) {
        //    layer.msg("请先进行人机验证");
        //    return;
        //}

        var remember = $("#rememberMe").checked;
        var returnUrl = '@ViewData["returnUrl"]';
        $("#btnLogin").text("登录中...");
        $("#btnLogin").attr("disabled", "disabled");
        $.post("LogOn",
            {
                "UserName": username,
                "Password": password,
                "RememberMe": remember,
                "Recaptcha": "123",
                'RecaptchaType': "None"
            },
            function(data, status) {
                if (status === "success") {
                    if (!data) {
                        if (returnUrl) {
                            location.href = returnUrl;
                        } else {
                            location.href = "/Admin/Home/Index";
                        }
                    } else {
                        layer.msg(data);
                        $("#inputPassword").val('');
                        $("#btnLogin").removeAttr("disabled");
                        $("#btnLogin").text("登录");
                    }
                } else {
                    layer.msg("登录失败,请稍后重试");
                    $("#inputPassword").val('');
                    $("#btnLogin").removeAttr("disabled");
                    $("#btnLogin").text("登录");
                }
            });
    }

    $("#btnLogin").on("click", Login);
    document.onkeydown = function(e) {
        if (e.keyCode === 13) {
            Login();
        }
    }
    </script>
</body>
</html>